package com.um.jdy.common.base.service.impl;


import com.um.jdy.common.base.dao.SettingDao;
import com.um.jdy.common.base.entity.dto.BaseInfoDTO;
import com.um.jdy.common.base.entity.po.Setting;
import com.um.jdy.common.base.entity.vo.BaseInfoVO;
import com.um.jdy.common.base.enums.SettingType;
import com.um.jdy.common.base.service.SettingService;
import com.um.springboot.starter.orm.entity.BaseDTO;
import com.um.springboot.starter.orm.entity.BaseVO;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.springboot.starter.utils.ClassUtils;
import com.um.springboot.starter.utils.NameUtils;
import com.um.springboot.starter.utils.ReflectUtil;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

/**
 * 服务实现类：系统设置表
 */
@Service
@Slf4j
public class SettingServiceImpl extends BaseEntityServiceImpl<SettingDao,Setting>  implements SettingService {

    /**
     * 查询设置
     *
     * @param type
     * @return
     */
    @Override
    public Map<String, String> getObject(String type) {
        Map<String,String> entity = new Hashtable<>();
        List<Setting> list = dao.getList("type",type);
        for(Setting item : list){
            entity.put(item.getProName(),item.getProValue());
        }
        return entity;
    }

    /**
     * 保存设置
     *
     * @param entity
     * @param type
     * @return
     */
    @Override
    public Boolean saveObject(Map entity, String type) {
        List<Setting> list = new ArrayList<>();

        Iterator it = entity.keySet().iterator();
        while (it.hasNext()){
            String proName = it.next().toString();
            String proValue = StringUtil.getTrim(entity.get(proName));

            Setting item = new Setting();
            item.setProName(proName);
            item.setProValue(proValue);
            item.setType(type);
            item.setBaseSettingId(createUUIDPrimaryId());
            list.add(item);
        }
        commonDao.deleteTrue(dao.getTableName(),"type",type);
        return dao.add(list);
    }

    private void listToVO(BaseVO baseVO,List<Setting> list){
        Map<String,String> entity = new Hashtable<>();
        for(Setting item : list){
            entity.put(item.getProName(),StringUtil.getTrim(item.getProValue()));
        }

        BeanInfo beanInfo = ClassUtils.getSelfBeanInfo(baseVO.getClass());
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

        for (PropertyDescriptor pd : pds) {
            String columnName =  NameUtils.getColumnName(pd.getName());
            Method writeMethod = pd.getWriteMethod();
            if(null == writeMethod){
                continue;
            }

            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                writeMethod.setAccessible(true);
            }
            try {
                ReflectUtil.setValue(baseVO,pd.getName(),StringUtil.getTrim(entity.get(columnName)));

            } catch (Exception e) {
                log.debug("listToVO",e);
            }
        }
    }

    private void dtoToList(BaseDTO baseDTO, List<Setting> list,String type){
        BeanInfo beanInfo = ClassUtils.getSelfBeanInfo(baseDTO.getClass());
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

        for (PropertyDescriptor pd : pds) {
            String columnName =  NameUtils.getColumnName(pd.getName());
            Method writeMethod = pd.getWriteMethod();
            if(null == writeMethod){
                continue;
            }
            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                writeMethod.setAccessible(true);
            }
            try {
                String value = (String)ReflectUtil.getValue(baseDTO,pd.getName());
                Setting item = new Setting();
                item.setProName(columnName);
                item.setProValue(value);
                item.setType(type);
                item.setBaseSettingId(createUUIDPrimaryId());
                list.add(item);
            } catch (Exception e) {
                log.debug("listToVO",e);
            }
        }
    }

    /**
     * 查询基础信息
     * @return
     */
    @Override
    public BaseInfoVO getBaseInfo(){
        BaseInfoVO baseInfoVO = new BaseInfoVO();
        List<Setting> list = dao.getList("type",SettingType.BaseInfo.getKey());
        listToVO(baseInfoVO,list);
        return baseInfoVO;
    }

    /**
     * 保存基础信息
     * @return
     */
    @Override
    public Boolean saveBaseInfo(BaseInfoDTO baseInfoDTO){
        List<Setting> list = new ArrayList<>();
        dtoToList(baseInfoDTO,list,SettingType.BaseInfo.getKey());
        commonDao.deleteTrue(dao.getTableName(),"type",SettingType.BaseInfo.getKey());
        return dao.add(list);
    }
}
